package com.sjf.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjf.common.CustomException;
import com.sjf.reggie.dto.SetmealDto;
import com.sjf.reggie.entity.Category;
import com.sjf.reggie.entity.Setmeal;
import com.sjf.reggie.entity.SetmealDish;
import com.sjf.reggie.mapper.SetmealMapper;
import com.sjf.reggie.service.CategoryService;
import com.sjf.reggie.service.SetmealDishService;
import com.sjf.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <huangtang>
 * @since 2022/4/26 12:05
 **/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Override
    @Transactional
    public void saveWithDish ( SetmealDto setmealDto ) {
        //先保存套餐主表
        save (setmealDto);
        //再保存到setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes ();
        setmealDishes = setmealDishes.stream ().map ((item)->{
            item.setSetmealId (setmealDto.getId ());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch (setmealDishes);
    }

    @Override
    public Page getList ( Integer page , Integer pageSize , String name ) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<> ();
        wrapper.lambda ().like (name!=null,Setmeal::getName,name);
        Page<Setmeal> pageInfo = new Page (page , pageSize);
        page(pageInfo,wrapper);
        Page<SetmealDto> setmealDtoPage = new Page<> ();
        BeanUtils.copyProperties (pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords ();
        List<SetmealDto> setmealList = records.stream ().map ((item)->{
            SetmealDto setmealDto = new SetmealDto ();
            BeanUtils.copyProperties (item,setmealDto);
            Category category = categoryService.getById (item.getCategoryId ());
            setmealDto.setCategoryName (category.getName ());
            return setmealDto;
        }).collect (Collectors.toList ());
        setmealDtoPage.setRecords (setmealList);
        return setmealDtoPage;
    }

    @Override
    @Transactional
    public void updateStatus ( Integer status , String ids ) {
        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<> ();

        String[] split = ids.split (",");

        for ( String id : split ) {
            wrapper.lambda ().set (status != null , Setmeal::getStatus , status)
                    .eq (Setmeal::getId , id);
            update (wrapper);
            wrapper.clear ();
        }
    }

    @Override
    @Transactional
    public boolean deleteByIds ( String ids ) {
        String[] split = ids.split (",");
        boolean result = true;
        for ( String id : split ) {
            Setmeal setmeal = getById (id);
            //正在售卖的套餐不能删除
            if(setmeal.getStatus () == 1){
                throw new CustomException (setmeal.getName ()+ " 套餐正在售卖，请停售后再删除!");
            }
            //先删除套餐
            result ^= removeById (id);
            //再删除套餐对应的菜品
            UpdateWrapper<SetmealDish> wrapper = new UpdateWrapper<> ();
            wrapper.lambda ().eq (SetmealDish::getSetmealId,id);
            result ^= setmealDishService.remove (wrapper);
        }
        return result;
    }

    @Override
    public SetmealDto getSetmealWithDish ( Long id ) {
        SetmealDto setmealDto = new SetmealDto ();
        //查询套餐
        Setmeal setmeal = getById (id);
        //查询套餐相关的菜品
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<> ();
        wrapper.lambda ().eq (SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list (wrapper);
        BeanUtils.copyProperties (setmeal,setmealDto);
        setmealDto.setSetmealDishes (setmealDishList);
        return setmealDto;
    }

    @Override
    @Transactional
    public void editSetmeal ( SetmealDto setmealDto ) {
        //修改套餐主表
        updateById (setmealDto);
        //修改套餐关联的菜品信息
        //先删除
        //获取菜品id集合
        setmealDishService.remove (new QueryWrapper<SetmealDish> ().lambda().eq (SetmealDish::getSetmealId,setmealDto.getId ()));
        //再添加
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes ().stream ().map ((item)->{
            item.setSetmealId (setmealDto.getId ());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch (setmealDishList);

    }

    @Override
    public List<Setmeal> getSetmealDishList ( Setmeal setmeal ) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<> ();
        wrapper.lambda ().eq (setmeal.getCategoryId ()!=null,Setmeal::getCategoryId,setmeal.getCategoryId ())
                .eq (setmeal.getStatus ()!=null,Setmeal::getStatus,setmeal.getStatus ())
                .orderByDesc (Setmeal::getUpdateTime);
        return list(wrapper);
    }
}
